package cn.iocoder.yudao.module.lfd.dal.dataobject.appointmentitem;

import cn.iocoder.yudao.module.lfd.dal.dataobject.productitem.ProductItemDO;
import cn.iocoder.yudao.module.lfd.service.productitem.ProductItemService;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class AppointmentItemList {

    private List<AppointmentItemDO> appointmentItems;

    public AppointmentItemList(ProductItemService productItemService,List<Long> items) {
        this.appointmentItems =init(productItemService, items);
    }

    private  List<AppointmentItemDO>  init(ProductItemService productItemService, List<Long> items) {
        List<AppointmentItemDO> appointmentItems = new ArrayList<>();
        for (Long itemId : items) {
            ProductItemDO appointmentItem = productItemService.getProductItem(itemId);
            if (appointmentItem != null) {
                AppointmentItemDO appointmentItemDO = new AppointmentItemDO();
                appointmentItemDO.setMoney(appointmentItem.getMoney());
                appointmentItemDO.setCostTime(appointmentItem.getCostTime());
                appointmentItemDO.setName(appointmentItem.getName());
                appointmentItemDO.setIntegral(appointmentItem.getIntegral());
                appointmentItems.add(appointmentItemDO);
            }
        }
        return appointmentItems;
    }


    public BigDecimal money() {
        return this.appointmentItems.stream().map(v -> v.getMoney()).reduce(BigDecimal.ZERO, BigDecimal::add);
    }

    public Long costTime() {
        return appointmentItems.stream().mapToLong(v -> v.getCostTime()).sum();
    }

    public Double sumIntegral() {
        return appointmentItems.stream().mapToDouble(v -> v.getIntegral()).sum();
    }

    public String items() {
        return appointmentItems.stream().map(v -> v.getName()).collect(Collectors.joining(","));
    }

    public List<AppointmentItemDO> addAppointmentId(Long id) {
        for (AppointmentItemDO appointmentItem : this.appointmentItems) {
            appointmentItem.setAppointmentId(id);
        }
        return  appointmentItems;
    }
}
